/*
 *
 *
 *
 *
 */


var IE = document.all?true:false;

// If NS -- that is, !IE -- then set up for mouse capture
if (!IE) document.captureEvents(Event.MOUSEMOVE);

var X = 0;
var Y = 0;

function getMouseXY(e) {
  	if (IE) { // grab the x-y pos.s if browser is IE
    	X = event.clientX + document.body.scrollLeft;
    	Y = event.clientY + document.body.scrollTop;
  	} else {  // grab the x-y pos.s if browser is NS
    	X = e.pageX;
    	Y = e.pageY;
  	}  
  	// catch possible negative values in NS4
  	if (X < 0){X = 0;}
  	if (Y < 0){Y = 0;}  
  	// show the position values in the form named Show
  	// in the text fields named MouseX and MouseY
  	// document.Show.MouseX.value = tempX
  	// document.Show.MouseY.value = tempY
  	return true;
	}
	
	
	document.onmousemove = getMouseXY;



function AjaxDiv(parent, child, float_div, center_div, hide_all, str_text){
		
		var loading = 
			'<html>'+
			'<head><title></title></head>'+
			'<body>'+
			'	<table widht="100%" height="90%" border="0">'+
			'		<tr>'+
			'			<td valign="bottom" align="center" height="55%">'+
			'				<img src="images/ajax-loader.gif" border="0"/>' +
			'			</td>'+
			'		</tr>'+
			'		<tr>'+
			'			<td valign="middle" align="center" height="45%" class="cargando">'+
			'			</td>'+
			'		</tr>'+
			'	<table>'
			'</body>'+
			'</hmtl>';
		
		this.center_div = false;
		this.h_spac = 0; 
		this.v_spac = 0; 
		
		var IE = document.all?true:false;
		
		var tempX = 0;
		var tempY = 0;
		
		var div_parent = document.getElementById(parent);
		var div_child  = document.getElementById(child);
		var div_width  = '';
		var div_height = '';
		
		var scr_width  = screen.width;
		var scr_height = (screen.height) -  (IE?(window.screenTop):100);
		var http_request = false;
		var anchor;

		this.fnc_makeRequest  = fnc_makeRequest;
		this.fnc_divClose     = fnc_divClose;
		
				
		/**
		 * Hace la peticion al servidor
		 *
		 */	
    function fnc_makeRequest(url, param){
    	param = (param==undefined)?'':param;
    	
    	tempX = X;
			tempY = Y;
               
        if (window.XMLHttpRequest) { // Mozilla, Safari,...
            http_request = new XMLHttpRequest();
            if (http_request.overrideMimeType) {
                http_request.overrideMimeType('text/xml');
                // Ver nota sobre esta linea al final
            }
        } else if (window.ActiveXObject) { // IE
            try {
                http_request = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    http_request = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
            }
        }

        if (!http_request) {
            alert('Falla :( No es posible crear una instancia XMLHTTP');
            return false;
        }
       // http_request.onreadystatechange = fnc_setContent;
       // http_request.open('GET', url + param, true);
       // http_request.send(null);
        
        if(float_div){
	        if(center_div){
	        	fnc_divSize();
				fnc_centerDiv();
	        }else{
	        	setPosition();
	        }
	        
	        if(hide_all){
	        	fnc_hideAll();
	        }else{
	        	fnc_setZindex();
	        }
			fnc_showDiv();
		}	
		http_request.onreadystatechange = fnc_setContent;
        http_request.open('GET', url + param, true);
        http_request.send(null);
    }
    
    
    
    /**
     *
     *
     */
		function fnc_divClose(){
			document.getElementById(child).innerHTML = '';
			document.getElementById(parent).style.visibility = 'hidden';
			document.getElementById(parent).style.top = 0;
			document.getElementById(parent).style.left = 0;
		}
    
    
    /**
		 * Ajusta la respuesta del servidor al DIV
		 *
		 */	
    function fnc_setContent(){
    	if (http_request.readyState == 1) 
    		if(str_text!="")
				document.getElementById(child).innerHTML = loading;
    	if (http_request.readyState == 2) 
    		if(str_text!="")
				document.getElementById(child).innerHTML = loading;
    	if (http_request.readyState == 3) 
    		if(str_text!="")
				document.getElementById(child).innerHTML = loading;
			if (http_request.readyState == 4){ 
				if(http_request.status == 200){
					document.getElementById(child).innerHTML = http_request.responseText;
				}
				if(http_request.status == 404){
					document.getElementById(child).innerHTML = 'Pagina no encontrada';
				}
				if(http_request.status == 500){
					document.getElementById(child).innerHTML = 'Error interno del servidor';
				}
			}
				
		}
		
		
		
		/**
		 * Obtiene el tamanio del DIV
		 *
		 */	
		function fnc_divSize(){
			div_width  = document.getElementById(parent).style.width;
			div_height = document.getElementById(parent).style.height;
			div_width  = div_width.replace('px', '');
			div_height = div_height.replace('px', '');	
			div_width  = parseInt(div_width);
			div_height = parseInt(div_height);	
		}
		
		
		
		/**
		 * Centra el DIV al Screen del Navegador
		 *
		 */	
		function fnc_centerDiv(){
			var pos_x = (scr_width/2) - (div_width/2);
			var pos_y = (scr_height/2) - (div_height/2);
			
			document.getElementById(parent).style.left = (pos_x)+'px';
			document.getElementById(parent).style.top  = (pos_y)+'px'; 
		}
		
		
		
		/**
		 * Ajusta el atributo de visibilidad  del DIV
		 *
		 */	
		function fnc_showDiv(){
			if(document.getElementById(parent).style.visibility == 'hidden')
				document.getElementById(parent).style.visibility = 'visible';
		}
		
		
		
		/**
		 * Oculta todos los DIV antes de mostrar el DIV solicitado
		 *
		 */
		function fnc_hideAll(){
		
			var doc_lenght = document.all.length;
			var object = '';
			for(int_i=0 ; int_i<parseInt(doc_lenght); int_i++){
				object += document.all[int_i].tagName + " " + document.all[int_i].id + " \n";
				if(document.all[int_i].tagName == 'DIV'){
					if(document.getElementById(document.all[int_i].id).style.visibility == 'visible'){
						document.getElementById(document.all[int_i].id).style.visibility = 'hidden';
						document.getElementById(parent).style.zIndex = 0;
					}
				}
			}
		}
		
		
		
		/**
		 * 
		 *
		 */
		function fnc_setZindex(){
			var int_z = 0;
			var doc_lenght = document.all.length;
			var object = '';
			for(int_i=0 ; int_i<parseInt(doc_lenght); int_i++){
				try{
					document.all[int_i].tagName
					object += document.all[int_i].tagName + " " + document.all[int_i].id + " \n";
					if(document.all[int_i].tagName == 'DIV'){
						if(document.getElementById(document.all[int_i].id).style.visibility == 'visible'){
							int_z++;
							document.getElementById(parent).style.zIndex = int_z;
						}
					}
				}catch(ex){
					
				}
			}
		}
		
		
		
		/**
		 * Obtiene el elemento donde fue solicitado del DIV
		 *
		 */	
		function fnc_anchorPosition(){
			alert(anchor);
			while( anchor != null ) {
				tempY += anchor.offsetTop;
				tempX += anchor.offsetLeft;
				anchor = anchor.offsetParent;
			}
			
			alert(tempX + " " + tempY);
		}
		
		
		
		/**
		 * Posiciona el DIV al elemento donde fue solicitado
		 *
		 */
		function setPosition(){
			var pos_x = tempX;
			var pos_y = tempY;
			document.getElementById(parent).style.left = (pos_x + 15)+'px';
			document.getElementById(parent).style.top  = (pos_y + 0)+'px';
		
		}
}